package com.android.menu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.android.interfata.Login;
import com.android.interfata.R;

public class MenuLegal extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.menu_legal);
		
		/* username si logout din heade - le completez cu datele corespunzatoare */
        TextView user = (TextView) findViewById(R.id.userHeader);
        SharedPreferences logPref = getSharedPreferences("LoginPref", MODE_PRIVATE);
    	String username = logPref.getString("user", "username");
    	user.setText(username);		
    	
        TextView logout = (TextView) findViewById(R.id.logoutHeader);
		/**** delogare ****/
		logout = (TextView) findViewById(R.id.logoutHeader);
		logout.setOnClickListener(new View.OnClickListener() 
		{
            public void onClick(View v) 
            {
            	askOK();
            }
        });
		/**** end delogare ****/
		
		

		tableClick();
	}
	

	
    private void tableClick()
    {
    	Log.d("tab", "click");
    	TableLayout tab = (TableLayout)findViewById(R.id.menu_help_tab);
    	TableRow row0 = (TableRow) tab.getChildAt(0);
    	row0.setOnClickListener(new View.OnClickListener() 
		{
            public void onClick(View v) 
            {
            	Intent intent = new Intent(getApplicationContext(), MenuLegalPrivacy.class);
				startActivity(intent);
            }
        });
    	
    	TableRow row1 = (TableRow) tab.getChildAt(1);
    	row1.setOnClickListener(new View.OnClickListener() 
		{
            public void onClick(View v) 
            {
            	Intent intent = new Intent(getApplicationContext(), MenuLegalTerms.class);
				startActivity(intent);
            }
        });
    }
	
	
	/**** delogare ****/
	private void askOK()
	{
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which)
		    {
		        switch (which)
		        {
			        case DialogInterface.BUTTON_POSITIVE:
			        	delPref();
			        	gotoLogin();
			        	break;
	
			        case DialogInterface.BUTTON_NEGATIVE:
			            //No button clicked
			            break;
		        }
		    }
		};

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to logout?").setPositiveButton("Yes", dialogClickListener)
		    .setNegativeButton("No", dialogClickListener).show();
	}
	

	private void delPref ()
	{
    	// au nevoie de contextul aplicatiei deci nu le pot pune in clasa pentru delogare
    	SharedPreferences logPref = getSharedPreferences("LoginPref", MODE_PRIVATE);
    	SharedPreferences.Editor prefEditor = logPref.edit();
    	prefEditor.clear();
    	prefEditor.commit();
	}
	
	private void gotoLogin()
	{
		Intent intent = new Intent(getApplicationContext(), Login.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	
		finish();
	}
	/**** end delogare ****/
	
	/*     menu      */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	Intent intent;
        switch (item.getItemId()) 
        {
            case R.id.menu_about:     
            	intent = new Intent(getApplicationContext(), MenuAbout.class);
				startActivity(intent);
                break;
            case R.id.menu_help:     
            	intent = new Intent(getApplicationContext(), MenuHelp.class);
				startActivity(intent);
                break;
            case R.id.menu_legal: 
            	intent = new Intent(getApplicationContext(), MenuLegal.class);
				startActivity(intent);
            	break;
            case R.id.menu_contact:
            	intent = new Intent(getApplicationContext(), MenuContact.class);
				startActivity(intent);
            	break;
        }
        return true;
    }
	/*     end menu      */
}
